package XZK010105;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**·
 * 快递 E 栈(控制台简易版)
 *
 * @author bing.z 2021/5/27 10:51
 **/
public class ExpressDemo {
    private static Scanner input = new Scanner(System.in);
    /**
     * 使用数组模拟快递柜,不过数组删除不太好操作,这里直接使用 ArrayList
     */
    private static List<Express> expressList = new ArrayList<>();

    public static void main(String[] args) {
        while (true) {
            System.out.println("======欢迎使用 ztranscript 快递柜======");
            System.out.println("请输入您的身份：1-快递员，2-用户，0-退出");
            int role = input.nextInt();
            if (role == 1) {
                // 快递员操作
                courierToDo();
            } else if (role == 2) {
                // 用户操作
                userToDo();
            } else if (role == 0) {
                System.out.println("Bye，欢迎您再次使用！");
                System.exit(0);
            }
        }
    }

    private static void userToDo() {
        System.out.println("请输入取件码：");
        int code = input.nextInt();
        Express express = findExpressByCode(code);
        if (express == null) {
            System.out.println("未找到该快递!");
        } else {
            boolean remove = expressList.remove(express);
            System.out.println("取件" + (remove ? "成功！" : "失败！"));
        }
    }

    private static void courierToDo() {
        System.out.println("请选择操作：1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
        int option = input.nextInt();
        switch (option) {
            case 1:
                saveExpress();
                break;
            case 2:
                deleteExpress();
                break;
            case 3:
                modifyExpress();
                break;
            case 4:
                viewAllExpress();
                break;
            default:
                System.out.println("当前支持 1-4 操作!");
        }
    }

    private static void viewAllExpress() {
        System.out.println("所有快递信息如下：");
        if (expressList.size() == 0) {
            System.out.println("当前暂无快递信息~");
        } else {
            System.out.println("快递单号\t公司名称\t取件码");
            for (Express el : expressList) {
                System.out.println(el.getExpressCode() + "\t" + el.getCompany() + "\t" + el.getCode());
            }
            System.out.println("共 " + expressList.size() + " 件");
        }
    }

    private static void modifyExpress() {
        System.out.println("请输入要修改的快递单号：");
        int expressCodeOld = input.nextInt();
        Express express = findExpressByExpressCode(expressCodeOld);
        if (express == null) {
            System.out.println("未找到快递！");
        } else {
            System.out.println("请输入新的快递单号：");
            int expressCode = input.nextInt();
            System.out.println("请输入新的公司名称：");
            String company = input.next();
            express.setCompany(company);
            express.setExpressCode(expressCode);
            System.out.println("修改成功！");
        }
    }

    private static void deleteExpress() {
        System.out.println("请输入要删除的快递单号：");
        int expressCode = input.nextInt();
        Express express = findExpressByExpressCode(expressCode);
        if (express == null) {
            System.out.println("未找到快递！");
        } else {
            boolean remove = expressList.remove(express);
            System.out.println("删除" + (remove ? "成功！" : "失败！"));
        }
    }

    /**
     * 根据快递单号查找快递
     *
     * @param expressCode
     * @return
     */
    private static Express findExpressByExpressCode(int expressCode) {
        for (Express express : expressList) {
            if (express.getExpressCode() == expressCode) {
                return express;
            }
        }
        return null;
    }

    /**
     * 根据取件码查找快递
     *
     * @param code
     * @return
     */
    private static Express findExpressByCode(int code) {
        for (Express express : expressList) {
            if (express.getCode() == code) {
                return express;
            }
        }
        return null;
    }

    private static void saveExpress() {
        System.out.println("请输入快递单号：");
        int expressCode = input.nextInt();
        System.out.println("请输入公司名称：");
        String company = input.next();
        // 取件码随机产生
        int code = (int) (Math.random() * 10000);
        Express express = new Express(code, expressCode, company);
        expressList.add(express);
        System.out.println("快递已存入，取件码是：" + code);
    }

}

class Express {
    // 取件码
    private int code;
    // 快递单号
    private int expressCode;
    // 公司名称
    private String company;

    public Express() {
    }

    public Express(int code, int expressCode, String company) {
        this.code = code;
        this.expressCode = expressCode;
        this.company = company;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getExpressCode() {
        return expressCode;
    }

    public void setExpressCode(int expressCode) {
        this.expressCode = expressCode;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}
